JS-名字中间部分替换成星号 发表于 2020-04-30 | 分类于 JavaScript | 字数统计: 66 字 | 阅读时长 ≈ 1 分钟 123456789101112131415function formatName(name) { let newStr; if (name.length === 2) { newStr = name.substr(0, 1) + '*'; } else if (name.length > 2) { let char = ''; for (let i = 0, len = name.length - 2; i < len; i++) { char += '*'; } newStr = name.substr(0, 1) + char + name.substr(-1, 1); } else { newStr = name; } return newStr;}